package com.psds.quartz;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * author: cjianquan
 * date: 2016/8/30
 */
@Controller
@RequestMapping("/quartzLogController")
public class QuartzLogController {

    @Autowired
    private QuartzLogService quartzLogService ;

    @Autowired
    private CommonQuartz commonQuartz;

    @RequestMapping(value = "/goQuartzLogList")
    public String goQuartzLogList(ModelMap model) {
        return "quartz_log_list";
    }

    @RequestMapping("/queryQuartzLogList")
    @ResponseBody
    public String queryQuartzLogList(HttpServletRequest request, QuartzLogEntity quartzLogEntity,BaseDto baseDto){
        String logExectime_begin = request.getParameter("logExectime_begin");
        String logExectime_end = request.getParameter("logExectime_end");
        if (quartzLogEntity == null){
            quartzLogEntity = new QuartzLogEntity();
        }
        // 如果没有页码,则设置pageNo=1
        if (baseDto.getPageNo() == null || baseDto.getPageNo() < 1) {
            baseDto.setPageNo(1);
        }
        // 设置分页大小
        if (baseDto.getPageSize() == null) {
            baseDto.setPageSize(10);
        }
        Map<String, Object> map = new HashMap<String, Object>();
        List<QuartzLogEntity> mapList = new ArrayList<QuartzLogEntity>();
        int iTotal = 0;
        try{
            iTotal = quartzLogService.queryQuartzLogCount(quartzLogEntity,logExectime_begin,logExectime_end);
            if(iTotal > 0){
                int iStart = (baseDto.getPageNo()-1)*baseDto.getPageSize();
                mapList = quartzLogService.queryQuartzLogList(quartzLogEntity, iStart, baseDto.getPageSize(),logExectime_begin,logExectime_end);
            }
        } catch(Exception ex) {
            ex.printStackTrace();
        }
        map.put("totalCount", iTotal);
        map.put("items", mapList);
        return JacksonUtil.bean2Json(map);
    }



}
